﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FPSLog : MonoBehaviour
{

    public  UnityEngine.UI.Text textObj_fps;
    public  bool                showFPS        = true;
    private float               updateInterval = 0.5f;
    private float               accum          = 0.0f; // FPS accumulated over the interval
    private float               frames         = 0f;   // Frames drawn over the interval
    private float               timeleft;              // Left time for current interval

    void Start() { InvokeRepeating("SetType", 0.1f, 0.5f); }

    public void LoadScene(int index) { SceneManager.LoadScene(index); }

    void LateUpdate()
    {
        // CALCULATE FPS
        if (showFPS)
        {
            timeleft -= Time.deltaTime;
            accum    += Time.timeScale / Time.deltaTime;
            ++frames;

            // Interval ended - update GUI text and start new interval
            if (timeleft <= 0.0f)
            {
                // display two fractional digits (f2 format)
                timeleft = updateInterval;
                accum    = 0.0f;
                frames   = 0f;
            }
        }
        else
        {
            textObj_fps.text = "";
        }
    }

    void SetType()
    {
        if (textObj_fps != null && accum > 0f && frames > 0f)
        {
            textObj_fps.text = "FPS: " + (accum / frames).ToString("f0");
        }
    }

}

public class FPS : MonoBehaviour
{

    public UnityEngine.UI.Text textObj_fps;
    void                       Start() { InvokeRepeating("SetType", 0.1f, 0.1f); }

    void SetType()
    {
        if (textObj_fps != null)
        {
            textObj_fps.text = "FPS: " + (Time.timeScale / Time.deltaTime).ToString("f0");
        }
    }

}